package lab03.Test06;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.*;


public class Test06 {


    public static void main(String[] args) {
        String path = "E://hello.txt";

        RandomAccessFile randomAccessFile = null;

        Map<String, Integer> map = new HashMap<String, Integer>();
        try {
            randomAccessFile = new RandomAccessFile(path, "rw");
            String line = null;

            int count = 0;
            while ((line = randomAccessFile.readLine()) != null) {


                count++;
                if (map.get(line) == null) {

                    map.put(line, 1);
                } else {

                    map.put(line, new Integer(map.get(line).intValue() + 1));

                }

            }

            System.out.println("共出现了" + count + "个单词");
            System.out.println("出现了" + map.size() + "不同的单词");


            //这里将map.entrySet()转换成list
            List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());

            Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
                @Override
                public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {

                    //降序排列
                    return o2.getValue().compareTo(o1.getValue());

                    //return  o2.getValue().compareTo(o1.getValue());   升序排列
                }
            });
            for (Map.Entry<String, Integer> entry : list) {

                System.out.println(entry.getKey() + "---" + entry.getValue());


//                for (int i=0;i<str.length;i++ ){
//                    System.out.println(str[i]);
//                }*/
            }

            //map集合
            //key - value
            //hello   3
            //there   2
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
